package com.mj.processController;

import org.activiti.engine.*;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.RuntimeServiceImpl;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.ReadOnlyProcessDefinition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * author : cRyann
 *
 * @create 2017-10-09
 **/
@Controller
public class TaskController {

    @Autowired
    ProcessEngine _processEngine;
    @Autowired
    TaskService taskService;
    /**
     * 跳转至指定活动节点
     *
     * @param targetTaskDefinitionKey
     * @throws Exception
     */
    public void jump(String targetTaskDefinitionKey, String taskId) throws Exception {
        TaskEntity currentTask = (TaskEntity) _processEngine.getTaskService().createTaskQuery()
                .processInstanceId(taskId).singleResult();
        jump(currentTask, targetTaskDefinitionKey);
    }

    /**
     * @param currentTaskEntity       当前任务节点
     * @param targetTaskDefinitionKey 目标任务节点（在模型定义里面的节点名称）
     * @throws Exception
     */
    private void jump(final TaskEntity currentTaskEntity, String targetTaskDefinitionKey) throws Exception {
        final ActivityImpl activity = getActivity(_processEngine,
                currentTaskEntity.getProcessDefinitionId(), targetTaskDefinitionKey);

        final ExecutionEntity execution = (ExecutionEntity) _processEngine.getRuntimeService().createExecutionQuery()
                .executionId(currentTaskEntity.getExecutionId()).singleResult();

        final TaskService taskService = _processEngine.getTaskService();

        //包装一个Command对象
        ((RuntimeServiceImpl) _processEngine.getRuntimeService()).getCommandExecutor().execute(new Command<Void>() {
            @Override
            public Void execute(CommandContext commandContext) {

                //删除当前的任务
                //不能删除当前正在执行的任务，所以要先清除掉关联
                currentTaskEntity.setExecutionId(null);
                taskService.saveTask(currentTaskEntity);
                taskService.deleteTask(currentTaskEntity.getId(), true);
                //创建新任务
                execution.setActivity(activity);
                execution.executeActivity(activity);
                return null;
            }
        });
    }

    private ActivityImpl getActivity(ProcessEngine processEngine, String processDefId, String activityId) {
        ProcessDefinitionEntity pde = getProcessDefinition(processEngine, processDefId);
        return pde.findActivity(activityId);
    }

    private ProcessDefinitionEntity getProcessDefinition(ProcessEngine processEngine, String processDefId) {
        return (ProcessDefinitionEntity) ((RepositoryServiceImpl) processEngine.getRepositoryService())
                .getDeployedProcessDefinition(processDefId);
    }
}
